﻿using System;
using System.Data;
using System.Windows.Controls;
using SLS.ExClassLib.RTIService;
using SLS.Controls;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message; 
namespace SLS.Base 
{
    public partial class SLBC 
    {
        public virtual void ActivateMenuItem(object sender, ExEventArg exArg)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;
                DataSetEvent = new DataSet();
                DataSetEvent = (DataSet)a.Tag;
                Tracer.Verbose(("ActivateMenuItem." + EventKey + " Arrived").PadLeft(20, '-'), EventKey);
                switch (EventKey)
                {
                    case "OnEditEveryRecord":
                        OnEditEveryRecord(sender, exArg);
                        break;
                    case "OnListByMGDKEY":
                        OnListByMGDKEY(sender, exArg);
                        break;
                    case "OnListTable":
                        OnListTable(sender, exArg);
                        break;
                    case "OnEditSelectedRecord":
                        OnEditSelectedRecord(sender, exArg);
                        break;
                    case "OnRunSelectedRecord":
                        OnRunSelectedRecord(sender, exArg);
                        break;  
                    case "OnValidateRecord":
                        OnValidateRecord(sender, exArg);
                        break;
                    case "OnThisPass":
                        OnThisPass(sender, exArg);
                        break;
                    case "OnThisFail":
                        OnThisFail(sender, exArg);
                        break;
                    case "OnReturnResult":
                    case "OnSingleEnd":
                    case "OnFileExit":
                        a.Name = EventKey;
                        OnFileExit(sender, exArg);
                        break;
                    case "OnUpdateDone":
                        OnUpdateDone(sender, exArg);
                        break; 
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// To Start a new workflow in orde to edit the selected record.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnListTable(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = "OnList" + BaseTable;
                MenuTreeNode tn = null;
                DataRow m_ActiveDataRow = null;
                DataRow dwBaseQuery = DataAccessService.Instance.MapDataRow("QueryOnly", "DB_" + BaseTable + "_SELECT_BY_INSTANCEID", TableQueryCond, null, m_ActiveDataRow, null, null);
                MessageEx msg =null;//= PrepareTreeNode(EventKey, null);            
                DataSet tnSet = new DataSet();
                tnSet = (DataSet)DataAccessService.Instance.ImportDataTable(tnSet, dwBaseQuery);
                msg.Address = "HomeDocumentUI";
                msg.setEvent = tnSet; 
                msg.JobID = EventKey;
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public void OnListByMGDKEY(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = "OnList" + BaseTable;
                MenuTreeNode tn = null;
                DataRow m_ActiveDataRow = null;
                DataRow dwBaseQuery = DataAccessService.Instance.MapDataRow("QueryOnly", "DB_" + BaseTable + "_SELECT_BY_MGDKEY",
                    TableQueryCond, null, m_ActiveDataRow, null, null);
                MessageEx msg = null;// PrepareTreeNode(EventKey, null); 
                DataSet tnSet = new DataSet();
                tnSet = (DataSet)DataAccessService.Instance.ImportDataTable(tnSet, dwBaseQuery);
                msg.Address = "HomeDocumentUI";
                msg.setEvent = tnSet; 
                tn.DataSetEvent = msg.setEvent;
                msg.JobID = "OnMenuSelected";
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public void OnEditEveryRecord(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;  
                 
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public void OnEditSelectedRecord(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;
                MessageEx msg = null;// PrepareTreeNode(EventKey, null); 
                DataSet tnSet = new DataSet();
                DataGrid grid = new DataGrid();// (DataGrid)DocumentManagement.Instance.ActiveItemsControl; 
                //sun20121002
                foreach (DataRowView rowview in grid.SelectedItems)
                {
                    tnSet = (DataSet)DataAccessService.Instance.ImportDataTable(tnSet, rowview.Row);
                }
                msg.setEvent = tnSet; 
                msg.Address = "HomeDocumentUI";
                msg.JobID = "OnEditSelectedRecord";
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = BaseTable;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            } 
        } 
       
        public void OnRunSelectedRecord(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                DataSet tnSet = (DataSet)a.Tag;
                string EventKey = a.Name;
                MessageEx msg = null;// PrepareTreeNode(EventKey, null);
                msg.setEvent = tnSet; 
                msg.CallerPath = Address; 
                msg.Address = "HomeDocumentUI";
                msg.JobID = EventKey;
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = tnSet.DataSetName;
                msg.CallerUID = (string)a.ToolTip;
                //This message will send to the WFEditXX workflow.
                //So, the message first check whether the workflow exists or not.
                //if not exist, it will start the workflow at first. and save the data
                //after the workflow start, it will pass the data
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
       
        public void OnValidateRecord(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;
                MessageEx msg = null;// PrepareTreeNode(EventKey, null);
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public void OnThisPass(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;

                MessageEx msg = null;// PrepareTreeNode(EventKey, null);
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public void OnUpdateDone(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;
                MessageEx msg = null;// PrepareTreeNode(EventKey, null);
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public void OnThisFail(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;
                MessageEx msg = null;// PrepareTreeNode(EventKey, null);
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public virtual void OnFileExit(object sender, ExEventArg e)
        {
            try
            {
                Fluent.MenuItem a = (Fluent.MenuItem)sender;
                string EventKey = a.Name;

                MessageEx msg = null;// PrepareTreeNode(EventKey, null);

                DataSet tnSet = msg.setEvent;
                foreach (DataRow dwTemp in SetUI.Tables[BaseTable].Rows)
                {
                    dwTemp["STATUS"] = dwTemp["STATUS"].ToString().Replace("OnLoadingForeignKeyTable;", "");
                }

                ExRowBase.SaveActiveRecord(SetUI.Tables[BaseTable], msg);
                foreach (DataTable tbTemp in ExRowBase.DataSetConclusion(SetUI, BaseTable).Tables)
                {
                    if (!tbTemp.TableName.Contains("ForeignKeyList") && !tbTemp.TableName.Contains("GUIProfile"))
                    {
                        tnSet = (DataSet)DataAccessService.Instance.ImportDataTable(tnSet, tbTemp);
                    }
                }
                msg.setEvent = tnSet; 
                msg.Address = "HomeDocumentUI";
                msg.JobID = EventKey;
                msg.Tag = "YES";
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                msg.Tag = EventKey;
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, null);
                object data = null;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
    }
}